<template>
  <div class="login-box">
    <div class="bg_img_box">
      <img src="@/assets/login/背景.png" class="bg_img" alt=""/>
      <div class="left_box">
        <div class="top-concent">智慧园区运营平台</div>
        <img src="@/assets/login/图.png" class="right-bottom" alt=""/>
      </div>
    </div>

    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <div class="form-logo">
        <img src="@/assets/login/Group 2.png" alt="">
      </div>

      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <img class="left_icon" slot="prefix" src="@/assets/login/loginPageIcon(3).png" alt="">
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin"
        >
          <img class="left_icon" slot="prefix" src="@/assets/login/loginPageIcon(2).png" alt="">
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <div class="yzm_box">
          <el-input
            v-model="loginForm.code"
            auto-complete="off"
            placeholder="验证码"
            @keyup.enter.native="handleLogin"
          >
            <img class="left_icon" slot="prefix" src="@/assets/login/loginPageIcon(1).png" alt="">
          </el-input>
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>

      </el-form-item>
      <div>
        <el-checkbox
          v-model="loginForm.rememberMe"
        ><span style="font-size: 16px"> 三天内自动登录</span>

        </el-checkbox
        >
        <el-button
          class="login-btn"
          :loading="loading"
          size="medium"
          type="success"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </div>

    </el-form>
  </div>
</template>

<script>
import {getCodeImg} from '@/api/login'
import Cookies from 'js-cookie'
import {encrypt, decrypt} from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          {required: true, trigger: 'blur', message: '用户名不能为空'}
        ],
        password: [
          {required: true, trigger: 'blur', message: '密码不能为空'}
        ],
        code: [
          {required: true, trigger: 'change', message: '验证码不能为空'}
        ]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.codeUrl = 'data:image/gif;base64,' + res.img
        this.loginForm.uuid = res.uuid
      })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, {expires: 3})
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 3
            })
            Cookies.set('rememberMe', this.loginForm.rememberMe, {
              expires: 3
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              this.$router.push({path: this.redirect || '/'}).catch(() => {
              })
            })
            .catch(() => {
              this.loading = false
              this.getCode()
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-box {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

.bg_img_box {
  width: 100%;
  height: 100%;

  .bg_img {
    width: 100%;
    height: 100%;
  }

  .left_box {
    padding-top: 50px;
    width: 50%;
    height: 50%;
    position: absolute;
    top: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    font-size: 35px;
    font-family: PingFang SC-Bold, PingFang SC;
    font-weight: bold;
    color: #00BA09;

    img {
      margin-top: 26px;
      width: 100%;
    }

    transform: translateY(-50%);

  }

}


.login-form {
  padding: 60px;
  position: absolute;
  top: 50%;
  right: 12%;
  z-index: 999;
  transform: translateY(-50%);
  width: 460px;
  height: 570px;
  background: #FFFFFF;
  box-shadow: 0px 0px 35px 1px rgba(0, 186, 9, 0.1);

  .form-logo {
    width: 210px;
    margin: 0 auto 50px;

    img {
      width: 100%;
      display: block;
    }

  }
}

::v-deep .el-input__inner {
  font-size: 16px;
  padding-left: 66px !important;
}

::v-deep .el-input__prefix {
  margin-left: 15px;
  padding-right: 16px;
  display: flex;
  align-items: center;
  position: absolute;

  &:after {
    position: absolute;
    content: "";
    right: 0;
    top: 50%;
    width: 2px;
    height: 28px;
    background: #F5F5F5;
    transform: translateY(-50%);
  }
}

.login-form {
  ::v-deep .el-form-item {
    margin-bottom: 30px;
    height: 55px;

    input {
      height: 60px;
    }
  }
}


.yzm_box {
  display: flex;
  flex-direction: row;

  ::v-deep input {
    flex: 1 !important;
    height: 60px !important;
  }

  img {
    margin-left: 12px;
    display: block;
    height: 55px;
  }
}

.login-btn {
  margin-top: 25px;
  width: 100%;
  height: 55px;
  border-radius: 1px;
  background: #26D12E;
  font-size: 20px;
  font-family: PingFang SC-Medium, PingFang SC;
  font-weight: 500;
  color: #FFFFFF;
  line-height: 23px;
}

@media screen and (min-width: 1366px) {
  .left_box {
    margin-left: 200px;
  }
}

@media screen and (min-width: 1366px) and (max-width: 1920px) {
  .left_box {
    margin-left: 100px;
  }
  .login-form {
    right: 16%;
  }
}

@media screen and (min-width: 2000px) and (max-width: 4000px) {
  .login-form {
    right: 24%;
  }
}

@media screen and (max-width: 1000px) {
  .left_box {
    display: none !important;
  }
  .login-form {
    right: 50%;
    top: 50%;
    transform: translate(50%, -50%);
  }
}

.left_icon{
  width: 20px !important;
  height: 20px !important;
  margin-left: 0 !important;
}
</style>
